﻿using System;
using System.Web.UI.WebControls;
using MyLib.Web.UI;

namespace MyLib.Web.Pages {
	/// <summary>
	/// 
	/// </summary>
	public abstract class FulltextSearchPage : MyPage {
		/// <summary>
		/// 
		/// </summary>
		public const string KeywordsControlID = "KeywordsText";

		/// <summary>
		/// 
		/// </summary>
		public const string SearchResultControlID = "SearchResult";

		private System.Web.UI.WebControls.GridView m_resultCtrl;

		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			m_resultCtrl = this.FindDescendantControl<System.Web.UI.WebControls.GridView>(SearchResultControlID);

			if (m_resultCtrl != null) {
				var ds1 = new MyObjectDataSource();
				ds1.ID = "FulltextSearchResultDataSource";
				ds1.TypeName = typeof(MyLib.Data.FulltextProvider).AssemblyQualifiedName;
				ds1.SelectMethod = "Find";
				ds1.SelectCountMethod = "FindCount";

				var parameter1 = new MyControlParameter();
				parameter1.ControlID = KeywordsControlID;
				parameter1.PropertyName = "Text";
				parameter1.Name = "keywords";
				var parameter2 = new Parameter("orderColumns", TypeCode.String, "");
				ds1.SelectParameters.Add(parameter1);

				ds1.SortParameterName = "orderColumns";

				ds1.EnableCaching = false;
				ds1.EnablePaging = true;

				m_resultCtrl.Parent.Controls.Add(ds1);

				m_resultCtrl.DataSourceID = ds1.ID;
				m_resultCtrl.AutoGenerateColumns = false;
				m_resultCtrl.AllowPaging = true;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="source"></param>
		/// <param name="args"></param>
		/// <returns></returns>
		protected override bool OnBubbleEvent(object source, EventArgs args) {
			bool causesValidation = false;
			string validationGroup = string.Empty;

			if (args is CommandEventArgs) {
				IButtonControl commandSource = source as IButtonControl;
				if (commandSource != null) {
					causesValidation = commandSource.CausesValidation;
					validationGroup = commandSource.ValidationGroup;
				}

				var cargs = (CommandEventArgs)args;

				if (string.Equals(cargs.CommandName, "Query", StringComparison.OrdinalIgnoreCase)) {
					HandleQuery(causesValidation, validationGroup);
					return true;
				} else {
					return false;
				}
			} else {
				return false;
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="causesValidation"></param>
		/// <param name="validationGroup"></param>
		private void HandleQuery(bool causesValidation, string validationGroup) {
			if (!causesValidation || this.IsValid) {
				if (m_resultCtrl != null) {
					m_resultCtrl.PageIndex = 0;
					m_resultCtrl.DataBind();
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		protected string Highlight(string text) {
			var keywordsCtrl = Page.FindDescendantControl<System.Web.UI.Control>(KeywordsControlID) as System.Web.UI.ITextControl;

			if (keywordsCtrl == null) {
				return text;
			}

			var keywords = keywordsCtrl.Text.Trim();

			foreach (var kw in keywords.Split(' ', ',', ';')) {
				if (string.IsNullOrWhiteSpace(kw)) {
					continue;
				}

				text = text.Replace(kw, "<strong class=\"highlight\" >" + kw + "</strong>");
			}

			return text;
		}
	} // end of FulltextSearchPage.
}
